package com.ych.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @author yangchenghao
 */
public class ProxyFactory {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxy() {
        ClassLoader classLoader = this.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        return Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object result = null;
                try {
                    System.out.println("日志：方法" + method.getName() + ",参数：" + Arrays.toString(args));
                    result = method.invoke(target,args);
                    System.out.println("日志：方法" + method.getName() + ",结果:" + result);
                } catch (Exception e){
                    e.printStackTrace();
                    System.out.println("日志：方法" + method.getName() + ",异常:" + e);
                }finally {
                    System.out.println("方法执行完毕");
                }
                return result;
            }
        });
    }
}
